# @Author ZhangGJ
# @Date 2021/11/30 10:41

import cv2.cv2 as cv2
import numpy as np


# 按位与
def bitwise_and():
    flower = cv2.imread('../images/amygdalus triloba.png')
    mask = np.zeros(flower.shape, np.uint8)
    mask[120:180, :, :] = 255
    mask[:, 80:180, :] = 255
    img = cv2.bitwise_and(flower, mask)
    cv2.imshow('flower', flower)
    cv2.imshow('mask', mask)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 按位或
def bitwise_or():
    flower = cv2.imread('../images/amygdalus triloba.png')
    mask = np.zeros(flower.shape, np.uint8)
    mask[120:180, :, :] = 255
    mask[:, 80:180, :] = 255
    img = cv2.bitwise_or(flower, mask)
    cv2.imshow('flower', flower)
    cv2.imshow('mask', mask)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 按位取反
def bitwise_not():
    flower = cv2.imread('../images/amygdalus triloba.png')
    img = cv2.bitwise_not(flower)
    cv2.imshow('flower', flower)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 按位异或
def bitwise_xor():
    flower = cv2.imread('../images/amygdalus triloba.png')
    m = np.zeros(flower.shape, np.uint8)
    m[120:180, :, :] = 255
    m[:, 80:180, :] = 255
    img = cv2.bitwise_xor(flower, m)
    cv2.imshow('flower', flower)
    cv2.imshow('mask', m)
    cv2.imshow('img', img)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 加密解密
def encrypt_decode():
    flower = cv2.imread('../images/amygdalus triloba.png')
    rows, columns, channel = flower.shape
    img_key = np.random.randint(0, 256, (rows, columns, 3), np.uint8)
    cv2.imshow('1', flower)
    cv2.imshow('2', img_key)
    result = _encode(flower, img_key)
    cv2.imshow('3', result)
    result = _encode(result, img_key)
    cv2.imshow('4', result)
    cv2.waitKey()
    cv2.destroyAllWindows()


def _encode(img, img_key):
    result = img = cv2.bitwise_xor(img, img_key)
    return result


if __name__ == '__main__':
    encrypt_decode()
